﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConnectionInfo.cs" company="">
//   
// </copyright>
// <summary>
//   The connection info.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace NetworkSupervisor.Helper
{
    using System.IO;
    using System.Windows.Forms;

    using DevExpress.Xpo.DB;

    /// <summary>
    ///   The connection info.
    /// </summary>
    public class ConnectionInfo
    {
        #region Constructors and Destructors

        /// <summary>
        ///   Initializes a new instance of the <see cref="ConnectionInfo" /> class.
        /// </summary>
        public ConnectionInfo()
        {
            this.Type = ConfigHelper.GetAppSettings("type");
            this.Server = ConfigHelper.GetAppSettings("server");
            this.Userid = ConfigHelper.GetAppSettings("userid");
            this.Password = ConfigHelper.GetAppSettings("password");
            this.Database = ConfigHelper.GetAppSettings("database");

            if (this.Password.EndsWith("="))
            {
                this.Password = DESEncryptHelper.Decrypt(this.Password);
            }
            else
            {
                ConfigHelper.SetAppSettings("password", DESEncryptHelper.Encrypt(this.Password));
            }
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets the connection string.
        /// </summary>
        public string ConnectionString
        {
            get
            {
                if (this.Type == "mssql")
                {
                    if (this.Server == ".")
                    {
                        return MSSqlConnectionProvider.GetConnectionString(this.Server, this.Database);
                    }

                    return MSSqlConnectionProvider.GetConnectionString(this.Server, this.Userid, this.Password, this.Database);
                }

                if (this.Type == "access")
                {
                    return AccessConnectionProvider.GetConnectionString(Path.Combine(Application.StartupPath, this.Database));
                }

                return string.Empty;
            }
        }

        /// <summary>
        ///   Gets or sets the database.
        /// </summary>
        public string Database { get; set; }

        /// <summary>
        ///   Gets or sets the password.
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        ///   Gets or sets the server.
        /// </summary>
        public string Server { get; set; }

        /// <summary>
        ///   连接类型：mssql、access
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        ///   Gets or sets the userid.
        /// </summary>
        public string Userid { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///   The update connection string.
        /// </summary>
        public void UpdateConnectionString()
        {
            ConfigHelper.SetAppSettings("type", this.Type);
            ConfigHelper.SetAppSettings("server", this.Server);
            ConfigHelper.SetAppSettings("userid", this.Userid);
            ConfigHelper.SetAppSettings("password", DESEncryptHelper.Encrypt(this.Password));
            ConfigHelper.SetAppSettings("database", this.Database);
        }

        #endregion
    }
}